package searchandsort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;


public class LargestNumber179 {
    public String largestNumber(int[] nums) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            list.add(nums[i]);
        }
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                StringBuilder sb1 = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                sb1.append(o1).append(o2);
                sb2.append(o2).append(o1);
                for (int i = 0; i < sb1.length(); i++) {
                    int s = sb2.charAt(i) - sb1.charAt(i);
                    if (s != 0) {
                        return s;
                    }
                }
                return 0;
            }
        });

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            result.append(list.get(i));
        }
        if (result.charAt(0) == '0') {
            return "0";
        }
        return result.toString();
    }
}
